function GDataLogin() 
{
  this.scope = 'http://docs.google.com/feeds';
  //this.scope = 'https://www.google.com/m8/feeds%20http://www.google.com/calendar/feeds';
  this.token = null;
  this.user = "";
  this.contacts = [];
}

GDataLogin.prototype = {

  logIn: function() {
    this.token = google.accounts.user.login(this.scope);
  },

  logOut: function() {
    google.accounts.user.logout();
  },

  loginCheck: function() {
    var token = google.accounts.user.checkLogin(this.scope);
    return token;
  },
          
  initFunc: function() {
  },

  fetchContacts: function(start, max, callback, error) {
    if(this.loginCheck()) {
      var contactsService = new google.gdata.contacts.ContactsService('test');
      var docService = new google.gdata.document.DocumentService('test');
      var contactsFeedUri = 'https://www.google.com/m8/feeds/contacts/default/full';
      var query = new google.gdata.contacts.ContactQuery(contactsFeedUri);
      var obj = this;
      
      query.setStartIndex(start);
      // Set the maximum of the result set to be 5
      query.setMaxResults(max);
      
      //the callback will be called with 'this' being the window object, we want it
      //to called with this === gd obj. so we create a anon. func to call obj.func()
      //don't know if this is the best way to do it.
      contactsService.getContactFeed(query, 
          function(res) {obj.handleContactsFeed(res)}, 
          function(res) {obj.handleError(res)});
    }
    this.contactCallback = callback;
    this.contactError = error;
  },

  handleContactsFeed: function(result) {
    var entries = result.feed.entry;
    this.user = result.feed.author[0].email.$t;
    var contacts = [];

    for (var i = 0; i < entries.length; i++) {
      var contactEntry = entries[i];
      var emailAddresses = contactEntry.getEmailAddresses();
      
      for (var j = 0; j < emailAddresses.length; j++) {
        var emailAddress = emailAddresses[j].getAddress();
        contacts.push(emailAddress);
        //$("#emails").append(emailAddress + "<br>");
      }    
    }

    this.contactCallback(contacts);
    //$("#loginmsg").html("Welcome, " + gd.getUser());

  },

  handleError: function(e) {
    this.contactError(e);
    alert(e.cause ? e.cause.statusText : e.message);
  },

  getUser: function() {
    return this.user;
  },

  getContacts: function() {
    return this.contacts;
  },

  fetchDocs: function(start, max, callback, error) {
  }

}
